export function query(collection, target, match = {}, group = {
  "region": "$region",
  "grid": "$grid",
  "date": "$date"
}) {
  if (!collection) {
    throw new Error('需要指定查询表名');
  }
  return window.cloudApp
    .callFunction({
      // 云函数名称
      name: "aggregate",
      // 传给云函数的参数
      data: {
        "target": target,
        "data": {
          "collection": collection,
          "match": match,
          "group": group,
        }
      }
    })
    .then((res) => {
      return Promise.resolve(res.result);
    })
    .catch(console.error);
}
window.query = query;
export const Collection = [
  'kuandai_zt',
  'kuandai_xp',
  'kuandai_gd',
  'xinyewu_zt',
  'xinyewu_xp',
  'xinyewu_gd',
  'tousu_zt',
  'tousu_xp',
  'tousu_gd',
]

const Collection2 = [
  'chongfutousulv',// 重复投诉率  
  'fanxiangrenshu', // 返乡人数
  'dianshihuoyueshu', // 电视沉默预计转活跃客户数量
  'kuandai_canpinglv',//   宽带参评率   
  'kuandai_huidanlv',// 宽带回单率
  'kuandai_jishilv',// 宽带及时率
  'kuandai_renjunchanneng',// 宽带人均产能
  'kuandai_zaituguidangbi',// 宽带在途归档比

  'tousu_bohuilv',// 投诉驳回率
  'tousu_canpinglv',// 投诉参评率
  'tousu_jishilv',// 投诉及时率
  'tousu_renjunchanneng',// 投诉人均产能
  'tousu_zaituguidangbi',// 投诉在途归档比

  'xinyewu_huidanlv',// 新业务回单率
  'xinyewu_jishilv',// 新业务及时率
  'xinyewu_renjunchanneng', // 新业务人均产能
  'xinyewu_zaituguidangbi',// 新业务在途归档比  
  'xinyewu_zhijiantongguolv',// 新业务质检通过率

  'zaixianfengzhi', // 在线峰值

  // 'nl2022kuandai',
  // 'nl2023kuandai',
  // 'nl2024kuandai',这两个从其他数据生成
  // 'nl2022tousu',
  // 'nl2023tousu',
  // '2023kuandai_huidanlv', // 2023宽带回单率
  // 'nl2024tousu'这两个从其他数据生成

  'ruoguangbiaoxianzhi',
  'xinzengzhengzhiliang',
]

export const bizType = {
  kuandai: ['shoukuan', 'quanjiaxiang', 'jinniu', 'kaimenhong', 'chaochang', 'yujing'],
  xinyewu: ['zuwang', 'FTTR', 'kanjia', 'pinganxiangcun', 'mensuo', 'yujing'],
  tousu: ['dianshi', 'chaochang', 'yujing', 'vip'],
}

export const CollectionSchema = () => Collection.reduce((p, c) => {
  const [type, status] = c.split('_');
  p[c] = bizType[type].reduce((p, c) => {
    p[c] = [];
    return p;
  }, {});
  return p;
}, {})

export const Collection2Schema = () => Collection2.reduce((p, c) => {
  p[c] = [];
  return p;
}, {})


export const getTopStaff = (data) => {
  const target = 'action/getTopStaff';
  return window.cloudApp
    .callFunction({
      // 云函数名称
      name: "aggregate",
      // 传给云函数的参数
      data: {
        target,
        data
      }
    })
    .then((res) => {
      return Promise.resolve(res.result.data);
    })
    .catch(console.error);
}

export const getKuCun = () => {
  const target = 'action/getKuCun';
  return window.cloudApp
    .callFunction({
      name: "aggregate",
      data: {
        target
      }
    })
    .then((res) => {
      return Promise.resolve(res.result.data);
    })
    .catch(console.error);
}

export const getListZaiTu = (data) => {
  const target = 'action/getListZaiTu';
  return window.cloudApp
    .callFunction({
      name: "aggregate",
      data: {
        target,
        data
      }
    })
    .then((res) => {
      return Promise.resolve(res.result.data);
    })
    .catch(console.error);
}

export const staff = (target, data) => {
  return window.cloudApp
    .callFunction({
      name: "staff",
      data: {
        target,
        data
      }
    })
    .then((res) => {
      return Promise.resolve(res.result);
    })
    .catch(console.error);
}